// 保存用户
import request from "@/utils/request";
import {IListParams} from "@/api/types/common";
import {IChatParams, ICustomerInfo, ICustomerUnRead} from "@/api/types/customer";
import {IChatMessage} from "@/api/types/chat";
import {IChatFriend} from "@/types/store/customer";
import {store} from "@/store";


// 获取客服账号
export const getCustomerAccount = (id: number) => {
  return request<ICustomerInfo>({
    method: 'GET',
    url: `/customer/getAccount/${id}`,
  })
}

// 保存客服账号
export const saveCustomerAccount = (user: ICustomerInfo) => {
  return request({
    method: 'POST',
    url: `/customer/saveAccount`,
    data: user
  })
}

// 查询客服列表
export const queryCustomerAccount = (page: IListParams) => {
  return request<{
    total: number
    rows: ICustomerInfo[],
  }>({
    method: 'GET',
    url: `/customer/queryAccount`,
    params: page
  })
}

// 未读消息list
export const getCustomerUnreadMap = () => {
  return request<Array<ICustomerUnRead>>({
    method: 'GET',
    url: `/customer/getUnreadMap`,
  })
}

// 查询客户有多少消息，客服未读的
export const getCustomerUnreadCount = (username: string, openId: string,loginId :number) => {
  return request<Number>({
    method: 'GET',
    url: `/customer/getCustomerUnreadCount`,
    params: {
      username,
      openId,
      loginId
    }
  })
}
// 查询客服聊天消息
export const queryMessageList = (params: IChatParams) => {
  return request<IChatMessage[]>({
    method: 'GET',
    url: `/customer/queryMessageListByPC`,
    params
  })
}

// 获取客服的朋友列表
export const getFriendList = (openId: string) => {
  return request<IChatFriend[]>({
    method: 'GET',
    url: `/customer/getFriendList/${openId}`
  })
}

// 主动回复服务器已处理该条消息
export const handleOneMessage = (id: number, username: string) => {
  return request({
    method: 'POST',
    url: `/customer/handleMessage/${id}/${username}`
  })
}

// 移除客服与用户关系
export const removeRelation = (params:{openId: string, username: string}) => {
  return request({
    method: 'DELETE',
    url: `/customer/removeRelation`,
    params
  })
}


// 发送客服消息
export const sendKfMessage = (msg: IChatMessage) => {
  msg.from = store.state.customer.user.openId
  msg.to = store.state.customer.activeUser?.username
  return request<IChatFriend[]>({
    method: 'POST',
    url: `/customer/sendKfMessage`,
    data: msg,
  })
}